import type {
  FormItem,
  allDataType,
} from '../types/index.ts'

export const getOptions = (item: FormItem): any[] => {
  if (item?.config?.options) return item.config.options
  if (isRef(item.options)) {
    return item.options.value as any[] ?? []
  } else {
    return item.options ?? []
  }
}


export function useHandleChangeData(allChangeData: allDataType['allChangeData'], item: Ref<FormItem>, data: Ref<any>) {
  let isStop = false
  watchEffect(() => {
    if(!item.value.field) return
    if(isStop) return
    // TODO: 优化any这个类型
    const options = getOptions(item.value) as any
    if(!options.length) return
    const value = data.value[item.value.field]
    const optionAt = options.find((v: any) => v.value == value)
    const label = optionAt ? optionAt.label : (value ?? '')
      allChangeData[item.value.field] = { optionAt, value, label, options}
  })
  
  function specific(specificValue: any) {
    isStop = true
    allChangeData[item.value.field] = specificValue
  }
  return [ specific ]
}